//
//  VideoListViewController.m
//  jiyujiaProgram
//
//  Created by 中良 赵 on 15/12/15.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "VideoListViewController.h"
#import "TuijianShipinTableViewCell.h"
#import "Common.h"
#import "VideoModel.h"

@interface VideoListViewController ()
{
    NSArray*arr;
    NSInteger _index;

}
@end

@implementation VideoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _lblTitleText.text=_CATEGORY;
    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    _tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    if ([_CATEGORY length]&&[_CLASSIFICATION length]) {
        [_tableview.mj_header beginRefreshing];
    }else{
        [SVProgressHUD showSuccessWithStatus:@"暂无视频"];
    }
}
-(void)requestData{
    NSDictionary *param = @{
                            @"CLASSIFICATION":_CLASSIFICATION,
                            @"CATEGORY":_CATEGORY,
                            @"STICK":@"1",
                            @"pageIndex":[NSString stringWithFormat:@"%ld",(long)_index],
                            @"pageSize":@"20",
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(QUERYVIDEOBY) params:param complete:^(BOOL successed, NSDictionary *result) {
        NSLog(@"result===%@",result);
        if (successed) {
            arr=[[NSArray alloc]init];
            arr=[VideoModel arrayOfModelsFromDictionaries:[result objectForKey:@"data"]];
            NSLog(@"dic====%@",arr);
            if (arr.count==0) {
                [SVProgressHUD showSuccessWithStatus:@"暂无视频"];
            }
            [_tableview reloadData];
        }
        else{
            [SVProgressHUD showErrorWithStatus:@"请求失败，请重试"];
        }
        [_tableview.mj_footer endRefreshing];
        [_tableview.mj_header endRefreshing];


    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)headerRefresh
{
    _index = 0;
    [self requestData];
}

- (void)footerRefresh
{
    if (arr.count%10 == 0) {
        _index ++;
        [self requestData];
    }else{
        [_tableview.mj_footer endRefreshing];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150.f;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    
    TuijianShipinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[[NSBundle mainBundle] loadNibNamed:@"TuijianShipinTableViewCell" owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (arr.count) {
        VideoModel *vmodel=[arr objectAtIndex:indexPath.row*2];
        cell.lblDetail1.text=vmodel.category_NAME;
        cell.lblName1.text=vmodel.video_NAME;
        cell.btn1.tag=indexPath.section*10;
        [cell.btn1 addTarget:self action:@selector(playVideoAction:) forControlEvents:UIControlEventTouchUpInside];
        VideoModel *vmodel1=[arr objectAtIndex:indexPath.row*2+1];
        cell.lblDetail1.text=vmodel1.category_NAME;
        cell.lblName1.text=vmodel1.video_NAME;
        cell.btn2.tag=indexPath.section*10+1;
        [cell.btn2 addTarget:self action:@selector(playVideoAction:) forControlEvents:UIControlEventTouchUpInside];
    }

    
    
    return cell;
}
-(void)playVideoAction:(id)sender{
    UIButton *btn=(UIButton*)sender;
    NSLog(@"%ld",(long)btn.tag);
    MPMoviePlayerViewController *playerViewController =[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:@"http://riyu.oss-cn-shanghai.aliyuncs.com/riyu/BDB9D790-2266-48A4-824F-5063E4223FD6.MOV"]];
    [self presentMoviePlayerViewControllerAnimated:playerViewController];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
